#import "DDZFViewController.h"
#import "DDCouponViewController.h"
#import "DDZFInfoCell.h"
#import "DDQianBao.h"
#import "DDFS.h"
#import "DDProduct.h"
#import "DDZFModel.h"
#import "DDWebView.h"
#import "DDCouponsModel.h"
#import "DDZFManager.h"
#import "TCCIAP.h"
@interface DDZFViewController ()<UICollectionViewDelegate, UICollectionViewDataSource,JTWFSDelegate>
{
    NSArray *arr2;
}
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) DDWebView *webView;
@property (nonatomic, strong) DDFS *FSView;
@property (nonatomic, strong) DDQianBao *qb;
@property (nonatomic, strong) CCLabel *tip;
@property (nonatomic, strong) DDButton *zfBtn;
@property (nonatomic, strong) DDProduct *product;
@property (nonatomic, strong) DDZFModel *model;
@property (nonatomic, strong) NSArray *arr;
@property (nonatomic, copy) NSString *yhID;
@property (nonatomic, assign) CGFloat collectionH;
@property (nonatomic, assign) CGFloat ysPrice;
@property (nonatomic, assign) CGFloat zkhouPrice;
@property (nonatomic, assign) CGFloat yhPrice;
@property (nonatomic, assign) CGFloat yhhouPrice;
@property (nonatomic, assign) CGFloat qbPrice;
@property (nonatomic, assign) CGFloat needQBPrice;
@property (nonatomic, assign) CGFloat qbhouPrice;
@property (nonatomic, assign) CGFloat gameZK;
@property (nonatomic, assign) CGFloat vipZK;
@property (nonatomic, assign) CGSize  size;
@end
@implementation DDZFViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.arr = @[@"道具:",@"原价格:",@"平台折扣:",@"VIP折上折:",@"折后价格:",@"优惠券:",@"需支付:"];
}
- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.bounces  = NO;
        [self.ddview addSubview:_scrollView];
        _scrollView.c_left.c_equal(self.ddview);
        _scrollView.c_right.c_equal(self.ddview);
        _scrollView.c_bottom.c_equal(self.ddview);
        _scrollView.c_top.c_equal(self.ddview.bar.c_bottom).with.coffset(GAP * 2);
    }
    return _scrollView;
}
- (DDWebView *)webView
{
    if (!_webView)
    {
        _webView = [[DDWebView alloc]init];
        [self.ddview addSubview:_webView];
        _webView.c_edges.c_equal(UIEdgeInsetsMake(JTWNaviBarHeight, 0, 0, 0));
        _webView.c_top.c_equal(self.ddview.bar.c_bottom);
    }
    return _webView;
}
- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:self.layout];
        [_collectionView registerClass:[DDZFInfoCell class] forCellWithReuseIdentifier:@"DDZFInfoCell"];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.scrollEnabled = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [self.scrollView addSubview:_collectionView];
        _collectionView.c_top.c_equal(self.scrollView);
        _collectionView.c_left.c_equal(self.scrollView).with.coffset(GAP);
        _collectionView.c_right.c_equal(self.ddview).with.coffset(-GAP);
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout *)layout
{
    if (!_layout)
    {
        _layout = [[UICollectionViewFlowLayout alloc] init];
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 0;
        _layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _layout;
}
- (DDQianBao *)qb
{
    if (!_qb)
    {
        _qb = [[DDQianBao alloc] init];
        _qb.selected = YES;
        [_qb addTarget:self action:@selector(dd_actionForXZQB) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:_qb];
        _qb.c_top.c_equal(self.collectionView.c_bottom).with.coffset(5);
        _qb.c_right.c_equal(self.ddview).with.coffset(-GAP);
        _qb.c_size.c_equal(self.qb.bounds.size);
    }
    return _qb;
}
- (DDButton *)zfBtn
{
    if (!_zfBtn)
    {
        _zfBtn = [[DDButton alloc] init];
        _zfBtn.backgroundColor = [DDHelper help].color;
        _zfBtn.layer.cornerRadius = GAP / 2.0;
        [_zfBtn addTarget:self action:@selector(dd_actionForZFBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:_zfBtn];
//                _zfBtn.c_top.c_equal(self.scrollView).with.coffset(SCREEN_HEIGHT - BUTTONHEIGHT - GAP * 2 - JTWNaviBarHeight);
        _zfBtn.c_bottom.c_equal(self.scrollView).with.coffset(-GAP);
        _zfBtn.c_left.c_equal(self.ddview).with.coffset(GAP);
        _zfBtn.c_right.c_equal(self.ddview).with.coffset(-GAP);
        _zfBtn.c_height.c_equal(BUTTONHEIGHT);
    }
    return _zfBtn;
}
- (DDFS *)FSView
{
    if (!_FSView)
    {
        _FSView = [[DDFS alloc] init];
        _FSView.delegate = self;
        _FSView.hidden = (self.zkhouPrice - self.yhPrice > self.qbPrice)?NO:YES;
        _FSView.jiage = [NSString stringWithFormat:@"还需支付%0.2f元",self.qbhouPrice];
        [self.scrollView addSubview:_FSView];
        _FSView.c_top.c_equal(self.qb.c_bottom).with.coffset(5);
        _FSView.c_left.c_equal(self.ddview).with.coffset(GAP);
        _FSView.c_right.c_equal(self.ddview).with.coffset(-GAP);
        _FSView.c_height.c_equal(JTWFSItemHEIGHT * 4);
    }
    return _FSView;
}
- (NSString *)yhID
{
    return (self.FSView.currentIndex == 3 ||self.yhPrice <= 0)?@"0":[self.model.coupons[[self.youhuiIndex intValue]] valueForKey:@"ID"];
}
- (CGFloat)gameZK
{
    return (self.FSView.currentIndex == 3 || [self.model.gamediscount floatValue] == 0)?1.0:([self.model.gamediscount floatValue] * 0.1);
}
- (CGFloat)vipZK
{
    return (self.FSView.currentIndex == 3 || [self.model.vipdiscount floatValue] == 0)?1.0:([self.model.vipdiscount floatValue] * 0.1);
}
- (CGFloat)ysPrice
{
    return [self.product.goodsPrice floatValue];
}
- (CGFloat)zkhouPrice
{
    return self.ysPrice * self.gameZK * self.vipZK;
}
- (CGFloat)yhPrice
{
    return (self.FSView.currentIndex == 3 || [self.youhuiIndex intValue] > self.model.coupons.count)?0:[[self.model.coupons[[self.youhuiIndex intValue]] valueForKey:@"value"] floatValue];
}
- (CGFloat)yhhouPrice
{
    if (self.FSView.currentIndex == 3)
        return self.ysPrice;
    else
        return (self.zkhouPrice >= self.yhPrice)?(self.zkhouPrice - self.yhPrice):0;
}
- (CGFloat)qbPrice
{
    return [self.model.balance floatValue];
}
- (CGFloat)needQBPrice
{
    return (self.FSView.currentIndex == 3 || self.qb.selected == NO)?0:(self.qbPrice >= self.yhhouPrice)?self.yhhouPrice:self.qbPrice;
}
- (CGFloat)qbhouPrice
{
    return (self.qb.selected == NO)?self.yhhouPrice:(self.yhhouPrice - self.needQBPrice);
}
- (CGSize)size
{
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        return self.view.bounds.size;
    }
    else
    {
        UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
        if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
            return CGSizeMake(SCREEN_MIN_LENGTH - GAP * 2, SCREEN_MIN_LENGTH);
        else
            return CGSizeMake(SCREEN_MIN_LENGTH, SCREEN_MIN_LENGTH - GAP * 2);
    }
}
- (void)setModel:(DDZFModel *)model
{
    if (_model != model)
    {
        _model = model;
        self.youhuiIndex = [NSString stringWithFormat:@"%u",self.model.coupons.count + 1000];
        self.collectionView.hidden = NO;
        self.layout.itemSize = [self dd_cellItemSize:[[UIApplication sharedApplication] statusBarOrientation]];
        self.qb.title = [NSString stringWithFormat:@"%.2f",self.qbPrice];
        self.FSView.fs = self.model.fs;
        self.FSView.jiage = [NSString stringWithFormat:@"还需支付%0.2f元",self.qbhouPrice];
        self.zfBtn.title = @"立即购买";
    }
}
+ (void)dd_show:(DDZFModel*)model product:(DDProduct *)product
{
    [CCHud startHud];
    DDZFViewController *vc = [[DDZFViewController alloc] init];
    [vc dd_pop:^{
        vc.product = product;
        vc.model = model;
    }];
}
- (CGSize)dd_cellItemSize:(UIInterfaceOrientation)orientation
{
    CGFloat width = 0;
    if (self.size.width + GAP * 2 == SCREEN_WIDTH)
    {
        width = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)?((self.size.width - GAP * 2) / 2.0):(self.size.width - GAP * 2);
        self.collectionH = (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)?JTWZFINFOCELLHEIGHT * 4 :JTWZFINFOCELLHEIGHT * 7;
    }
    else
    {
        width = (self.size.width - GAP * 2);
        self.collectionH = JTWZFINFOCELLHEIGHT * 7;
    }
    
    [self dd_scrollContentSize];
    _collectionView.c_height.c_equal(self.collectionH);
    
    return CGSizeMake(width , JTWZFINFOCELLHEIGHT);
}
- (void)dd_screenRotate:(UIInterfaceOrientation)orientation
{
    [super dd_screenRotate:orientation];
    self.layout.itemSize = [self dd_cellItemSize:orientation];
}
- (void)dd_scrollContentSize
{
    CGFloat H = (self.FSView.hidden)?(self.collectionH + self.qb.bounds.size.height + BUTTONHEIGHT + GAP * 3):(self.collectionH + self.qb.bounds.size.height + BUTTONHEIGHT + JTWFSItemHEIGHT * 4 + GAP * 4);
    CGFloat contentSizeH = MAX(self.size.height - JTWNaviBarHeight, H);
    self.scrollView.contentSize = CGSizeMake(self.size.width, contentSizeH);
    self.zfBtn.c_top.c_equal(self.scrollView).with.coffset(contentSizeH - BUTTONHEIGHT - GAP );
}
- (void)dd_actionForBackButton
{
    [self dismissViewControllerAnimated:YES completion:^{
        if (self.FSView.currentIndex == 3 || self.FSView.currentIndex == 2 )
        {
            [TCCIAP dd_checkQueueAndDB];
        }
    }];
    
}
- (void)dd_actionForXZQB
{
    self.qb.selected = !self.qb.selected;
    self.FSView.hidden = (self.qbPrice >= self.yhhouPrice && self.yhhouPrice > 0)?self.qb.selected:(self.yhhouPrice <= 0)?YES:NO;
    self.FSView.currentIndex = 0;
    self.FSView.jiage = [NSString stringWithFormat:@"还需支付%0.2f元",self.qbhouPrice];
    [self dd_scrollContentSize];
}
- (void)dd_actionForZFBtn
{
    if (self.FSView.currentIndex == 0 && self.qb.selected == NO && self.yhhouPrice > 0) return [CCAlert showMessage:@"请选择一种支付方式" hidden:nil];
    else if (self.FSView.currentIndex == 0 && self.qb.selected && self.qbhouPrice > 0) return [CCAlert showMessage:@"余额不足，请选择一种方式继续支付" hidden:nil];
    else if (self.FSView.currentIndex == 3 && [TCCIAP dd_isDoing])
    {
        [CCHud stopHud];
        [CCAlert showMessage:@"正在支付..." hidden:^{
            [CCHud startHud];
        }];
        return;
    }
    self.product.couponID = (self.FSView.currentIndex ==3)?@"0":self.yhID;
    self.product.balance = (self.FSView.currentIndex ==3)?@"0":[NSString stringWithFormat:@"%.2f",self.needQBPrice];
    self.product.couponval = (self.FSView.currentIndex ==3)?@"0":[NSString stringWithFormat:@"%.2f",self.yhPrice];
    self.product.realPrice = (self.FSView.currentIndex ==3)?[NSString stringWithFormat:@"%.2f",self.ysPrice]:[NSString stringWithFormat:@"%.2f",self.qbhouPrice];
    self.product.ZFChannel = [NSString stringWithFormat:@"%d",self.FSView.currentIndex];
    
    [DDZFManager dd_managerZF:self.product web:^(NSString *web_url) {
        if ([web_url length] > 1)
            [self.webView dd_show:web_url];
        else
        {
            [CCAlert showMessage:@"支付成功" hidden:^{
                [self dismissViewControllerAnimated:YES completion:^{
                    [CCNotice post:DDPlatformPayNotification obj:nil info:@{@"message":@"支付成功"}];
                }];
            }];
        }
    }];
    return;
    
}

#pragma mark CollectionViewDelegate & DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 7;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DDZFInfoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DDZFInfoCell" forIndexPath:indexPath];
    cell.index = (int)indexPath.row;
    cell.title = [NSString stringWithFormat:@"%@",self.arr[indexPath.row]];
    WS(self)
    cell.block = ^{
        DDCouponViewController *vc = [[DDCouponViewController alloc] init];
        vc.coupons = weakSelf.model.coupons;
        vc.discoupons = weakSelf.model.discoupons;
        vc.selectIndex = [weakSelf.youhuiIndex integerValue];
        vc.showUseBtn = YES;
        [weakSelf presentViewController:vc animated:YES completion:nil];
    };
    if (indexPath.row == 0)     cell.content = self.product.goodsTitle;
    else if (indexPath.row == 1) cell.content = [NSString stringWithFormat:@"%.2f元",self.ysPrice];
    else if (indexPath.row == 2) cell.content = (self.gameZK == 1)?@"无":[NSString stringWithFormat:@"%0.1f折",self.gameZK * 10];
    else if (indexPath.row == 3) cell.content = (self.vipZK == 1)?@"无":[NSString stringWithFormat:@"%0.1f折",self.vipZK * 10];
    else if (indexPath.row == 4) cell.content = [NSString stringWithFormat:@"%.2f元",self.zkhouPrice];
    else if (indexPath.row == 6) cell.content = [NSString stringWithFormat:@"%.2f元",self.yhhouPrice];
    else if (indexPath.row == 5)
    {
        if (self.model.coupons.count == 0 || self.FSView.currentIndex == 3)
            cell.content = @"无可用券>>";
        else
        {
            cell.content = ([self.youhuiIndex intValue] > self.model.coupons.count)?([NSString stringWithFormat:@"%lu张可用券>>",(unsigned long)self.model.coupons.count]):
            [NSString stringWithFormat:@"减%0.1f元>>",self.yhPrice];
        }
    }
    cell.userInteractionEnabled = (self.FSView.currentIndex == 3)?NO:YES;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
}
#pragma mark JTWFSDelegate
- (void)dd_actionForJTWFSDelegate
{
    [self.collectionView reloadData];
    self.FSView.jiage = [NSString stringWithFormat:@"还需支付%0.2f元",self.qbhouPrice];
    if (self.FSView.currentIndex == 3)
    {
        self.qb.selected = NO;
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self reload];
}
- (void)reload
{
    self.ddview.bar.title = @"购买信息";
    if (self.model)
    {
        [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:6 inSection:0],[NSIndexPath indexPathForRow:5 inSection:0]]];
        self.layout.itemSize = [self dd_cellItemSize:[[UIApplication sharedApplication] statusBarOrientation]];
        self.qb.selected = (self.yhhouPrice <= 0)?NO:YES;
        self.qb.userInteractionEnabled = (self.yhhouPrice <= 0)?NO:YES;
        self.FSView.hidden = (self.qbPrice >= self.yhhouPrice && self.yhhouPrice > 0)?self.qb.selected:(self.yhhouPrice <= 0)?YES:NO;
        self.FSView.jiage = [NSString stringWithFormat:@"还需支付%0.2f元",self.qbhouPrice];
        [self dd_scrollContentSize ];
    }
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [CCHud stopHud];
}
- (void)dealloc
{
    NSLog(@"=====dealloc zfviewcontroller");
    //    [[NSNotificationCenter defaultCenter] removeObserver:self name:TCCIAPNotification object:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
